package gui;

import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import LogicSide.SongResult;

public class PlayNow {
	private static int current_song_index;
	private static Browser browser;

	public static Composite createPlayNow(final Shell shell,
			final List<String> songsNames) {
		//update 
		Main.inSearch = false;
		
		Composite playNow = new Composite(shell, SWT.BORDER);
		playNow.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));

		GridLayout playNowLayout = new GridLayout(1, false);
		playNow.setLayout(playNowLayout);

		current_song_index = 0;
		// Title
		final Text songText = new Text(playNow, SWT.SINGLE | SWT.CENTER);
		songText.setLayoutData(new GridData(SWT.FILL, SWT.TOP, false, false, 1,
				1));
		songText.setText("You are watching " + songsNames.get(0));

		browser = new Browser(playNow, SWT.NONE);
		browser.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		new Thread(new Runnable() {

			@Override
			public void run() {
				try {
					final String url = SongResult.getSongYoutubeURL(songsNames
							.get(current_song_index));
					Main.display.asyncExec(new Runnable() {

						@Override
						public void run() {
							if (!browser.isDisposed()) {
								browser.setUrl(url);
							}

						}
					});
				} catch (Exception e) {
					Main.display.asyncExec(new Runnable() {

						@Override
						public void run() {
							Utils.popUpBox("Failed loading song",
									"Couldn't get song's url", shell);
						}
					});
				}

			}
		}).start();

		Composite navigateComposite = new Composite(playNow, SWT.NONE);
		navigateComposite.setLayoutData(new GridData(SWT.FILL, SWT.BOTTOM,
				false, false, 1, 1));
		navigateComposite.setLayout(new GridLayout(2, true));

		// navigate composite buttons
		// Previous song
		Button previousSongButton = new Button(navigateComposite, SWT.PUSH
				| SWT.FLAT);
		previousSongButton.setText("Previous song");
		previousSongButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
				true, 1, 1));
		//
		// next song
		Button nextSongButton = new Button(navigateComposite, SWT.PUSH
				| SWT.FLAT);
		nextSongButton.setText("Next song");
		nextSongButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
				true, 1, 1));
		//

		// buttons selection handlers
		// next button
		nextSongButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				current_song_index = (current_song_index + 1)
						% songsNames.size();
				new Thread(new Runnable() {

					@Override
					public void run() {
						try {
							final String url = SongResult
									.getSongYoutubeURL(songsNames
											.get(current_song_index));
							Main.display.asyncExec(new Runnable() {

								@Override
								public void run() {
									browser.setUrl(url);
									songText.setText("You are now watching "
											+ songsNames
													.get(current_song_index));
								}
							});

						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				}).start();
			}
		});
		//
		// previous button
		previousSongButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				if (current_song_index == 0)
					current_song_index = songsNames.size() - 1;
				else
					current_song_index--;
				new Thread(new Runnable() {

					@Override
					public void run() {
						try {
							final String url = SongResult.getSongYoutubeURL(songsNames
									.get(current_song_index));
							Main.display.asyncExec(new Runnable() {

								@Override
								public void run() {
									browser.setUrl(url);
									songText.setText("You are now watching " + songsNames.get(current_song_index));
								}
							});

						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				}).start();
			}
		});
		//

		return playNow;
	}
}
